package com.wb.mas.utils;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.wb.mas.BuildConfig;
import com.wb.mas.app.AppApplication;
import com.wb.mas.app.SPKeys;
import com.wb.mas.app.UUidUtils;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import androidx.core.content.ContextCompat;

import me.goldze.mvvmhabit.utils.StringUtils;
import okhttp3.RequestBody;

public class ParamsUtils {

    private static Map<String, String> commonParams;

//    public static String getJsonParams(Map<String, String> params) {
//
//
//        Map<String, Object> tempParams = new HashMap<>();
//        tempParams.putAll(params);
//        tempParams.put("channel", BuildConfig.channel);
//        Map<String, String> commonParams = getCommonParams();
//        if (commonParams != null) {
//            tempParams.put("commonParam", commonParams);
//        }
//        return new Gson().toJson(tempParams);
//    }

    public static String getJsonParams(Map<String, Object> params) {


        Map<String, Object> tempParams = new HashMap<>();
        tempParams.putAll(params);
        tempParams.put("channel", BuildConfig.channel);
        Map<String, String> commonParams = getCommonParams();
        if (commonParams != null) {
            tempParams.put("commonParam", commonParams);
        }
        if (!params.containsKey("userUuid")) {
            tempParams.put("userUuid", UUidUtils.getUUid());
        }
        if (!params.containsKey("addressBookUuid")) {
            tempParams.put("addressBookUuid", UUidUtils.getAddressBookUuid());
        }
        return new Gson().toJson(tempParams);
    }


    public static RequestBody getParamsBody(Map<String, Object> params) {
        return RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), getJsonParams(params));
    }

    public static Map<String, RequestBody> getFileBody(Map<String, Object> params) {
        Map<String, RequestBody> map = new HashMap<>();
        for (String key : params.keySet()) {
            map.put(key, RequestBody.create(okhttp3.MediaType.parse("text/plain; charset=utf-8"), String.valueOf(params.get(key))));
        }
        return map;
    }


//    {        //公参示例
//        "ad": "1555659502209-8378380770138480726",        // af 的id
//            "device_id": "00000000-1cd8-dde0-0000-00005c87785a",    //设备唯一码  自己拼装的
//            "imei": "",            //设备imei 信息 注意权限
//            "cv": "1.9.9",         //APK版本
//            "lg": "zh",            //语言系统
//            "ov": "7.1.1",         //系统版本
//            "osVersion": "25",     //系统版本号标识  android 6是23
//            "pn": "com.excellent.besar",   //包名
//            "sc": "1920*1080",     //屏幕尺寸
//            "ms": "organic",       //渠道来源 通过af获取的  默认organic
//            "bd": "Meizu PRO 6",   //设备型号
//            "os": "android"       //操作系统
//    }

    private static Map<String, String> getCommonParams() {
        if (commonParams == null || !commonParams.containsKey("Authorization")) {
            commonParams = new HashMap<>();

            commonParams.put("ad", "");
//            commonParams.put("device_id", UniqueUtils.getUniquePsuedoID());
            commonParams.put("cv", BuildConfig.VERSION_NAME + BuildConfig.VERSION_CODE);
            commonParams.put("ov", "");
            commonParams.put("imei", "");
            commonParams.put("sc", "");
            commonParams.put("bd", "");
            commonParams.put("osVersion", "");
            commonParams.put("lg", "in");
            commonParams.put("pn", BuildConfig.APPLICATION_ID);
            commonParams.put("ms", "");//
            commonParams.put("os", "android");
            String token = SPUtils.getInstance().getString(SPKeys.KEY_OF_TOKEN);
            if (!TextUtils.isEmpty(token)) {
                commonParams.put("Authorization", token);

            }
        }
        if (StringUtils.isEmpty(commonParams.get("ad")) && !StringUtils.isEmpty(SPUtils.getInstance().getString(SPKeys.APPS_FLYER_UID))) {
            commonParams.put("ad", SPUtils.getInstance().getString(SPKeys.APPS_FLYER_UID));
        }

        return commonParams;
    }


    public static String getImei() {
        TelephonyManager telephonyManager = (TelephonyManager) AppApplication.getInstance().getSystemService(Context.TELEPHONY_SERVICE);

        if (ContextCompat.checkSelfPermission(AppApplication.getInstance(), Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {

            String imei = telephonyManager.getDeviceId();
            return imei;
        }
        return "";
    }


    public static int getScreenWidth(Context context) {
// 获取屏幕分辨率
        WindowManager wm = (WindowManager) (context.getSystemService(Context.WINDOW_SERVICE));
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int mScreenWidth = dm.widthPixels;
        return mScreenWidth;
    }

    public static int getScreenHeight(Context context) {
// 获取屏幕分辨率
        WindowManager wm = (WindowManager) (context.getSystemService(Context.WINDOW_SERVICE));
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int mScreenHeigh = dm.heightPixels;
        return mScreenHeigh;
    }


    /**
     * 清除  token
     */
    public static void clearToken() {
        Map<String, String> commonParams = getCommonParams();
        if (commonParams != null) {
            commonParams.remove("Authorization");
        }
    }


}
